const app = getApp();

import { getIndexData,getRuralList, getCoupons, getTemlIds, getLiveList, } from '../../api/api.js';
import { CACHE_SUBSCRIBE_MESSAGE } from '../../config.js';
import Util from '../../utils/util.js';
import wxh from '../../utils/wxh.js';
import { getSeckillIndexTime, getSeckillList } from '../../api/activity.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '0',
      'title': '美丽乡村管理',
      'color': false
    },
    imgUrls: [],
    itemNew: [],
    activityList: [],
    menus: [],
    bastBanner: [],
    bastInfo: '',
    bastList: [],
    fastInfo: '',
    fastList: [],
    firstInfo: '',
    firstList: [],
    salesInfo: '',
    likeInfo: [],
    lovelyBanner: {},
    benefit: [],
    indicatorDots: false,
    circular: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    window: false,
    iShidden: false,
    navH: "",
    newGoodsBananr: '',
    selfLongitude: '',
    selfLatitude: '',
    liveList: [],
    liveInfo: {},
    /**新的定义 */
    scrollLeft: 263,
    cateList_test: [
      {
        id: 52,
        cate_name: '特价周边游',
        pid: 0,
      }, {
        id: 52,
        cate_name: '特价周边游',
        pid: 0,
      }, {
        id: 52,
        cate_name: '特价周边游',
        pid: 0,
      }, {
        id: 52,
        cate_name: '特价周边游',
        pid: 0,
      }, {
        id: 52,
        cate_name: '特价周边游',
        pid: 0,
      }
    ],
    cateList_yuan: [
      {
        id: 113,
        state: '已结束',
        status: 0,
        stop: 1591052400,
      },
      {
        id: 114,
        state: '已结束',
        status: 0,
        stop: 1591063200,
      },
      {
        id: 115,
        state: '已结束',
        status: 0,
        stop: 1591063200,
      },
      {
        id: 113,
        state: '已结束',
        status: 0,
        stop: 1591052400,
      },
      {
        id: 114,
        state: '已结束',
        status: 0,
        stop: 1591063200,
      },
      {
        id: 115,
        state: '已结束',
        status: 0,
        stop: 1591063200,
      },
      {
        id: 113,
        state: '已结束',
        status: 0,
        stop: 1591052400,
      },
      {
        id: 114,
        state: '已结束',
        status: 0,
        stop: 1591063200,
      },
      {
        id: 115,
        state: '已结束',
        status: 0,
        stop: 1591063200,
      },
      {
        id: 113,
        state: '已结束',
        status: 0,
        stop: 1591052400,
      },
      {
        id: 114,
        state: '已结束',
        status: 0,
        stop: 1591063200,
      },
      {
        id: 115,
        state: '已结束',
        status: 0,
        stop: 1591063200,
      },
      {
        id: 113,
        state: '已结束',
        status: 0,
        stop: 1591052400,
      },
      {
        id: 114,
        state: '已结束',
        status: 0,
        stop: 1591063200,
      },
      {
        id: 115,
        state: '已结束',
        status: 0,
        stop: 1591063200,
      }

    ],
    catId: 0,
    //设置timeList得到最后一个开始的值
    timeList: [],
    active: 5,
    /**控制是否继续加载数据 */
    loadend: false,
    page: 1,
    limit: 3,
    loading: false,
    loadTitle: '加载更多',//提示语
    row_count: 0,
    is_sheet: 1,
    actions: [
      { name: '选项', color: '#07c160' },
      { loading: true },
      { name: '禁用选项', disabled: true },
    ],
    radio: '1',
    color: '',
    navH: '',
    navbarShow: 'block'
  },
  onPageScroll: function (ev) {
    console.log('滚动');

    // var _this = this;
    // if (ev.scrollTop>10){
    //   _this.setData({
    //     navbarShow:'none'
    //   })
    // }else{
    //   _this.setData({
    //     navbarShow: 'block'
    //   })
    // }
    console.log('ev.scrollTop', ev.scrollTop);
    //当滚动的top值最大或最小时，为什么要做这一步是因为在手机实测小程序的时候会发生滚动条回弹，所以为了处理回弹，设置默认最大最小值
    // if (ev.scrollTop <= 0) {
    //   ev.scrollTop = 0;
    // } else if (ev.scrollTop > wx.getSystemInfoSync().windowHeight) {
    //   ev.scrollTop = wx.getSystemInfoSync().windowHeight;
    // }
    // //判断浏览器滚动条上下滚动
    // if (ev.scrollTop > this.data.scrollTop || ev.scrollTop == wx.getSystemInfoSync().windowHeight) {
    //   //向下滚动
    // } else {
    //   //向上滚动
    // }
  },
  touchStart: function (ev) {

  },
  /**滑动开始 */
  swiperStar: function (e) {
    var current = e.detail.current, imgUrls = this.data.imgUrls;
    var color = imgUrls[current]['color'];
    // this.setData({
    //   color
    // });

    var that = this;
    var animation = wx.createAnimation({
      duration: 2000,
      timingFunction: 'ease',
      delay: 50
    });
    animation.backgroundColor(color).step()
    this.setData({
      ani: animation.export()
    })

  },
  setGoodsSearch: function () {
    wx.navigateTo({
      url: '/pages/goods_search/index',
    })
  },

  onChange(event) {
    console.log('改变', event);
    this.setData({
      radio: event.detail,
    });
  },
  closeTip: function () {
    wx.setStorageSync('msg_key', true);
    this.setData({
      iShidden: true
    })
  },
  goCateOne: function (e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      page: 1,
    })
    this.getIndexConfig(id);

  },
  getSeckillConfig: function () {
    let that = this;
    getSeckillIndexTime().then(res => {
      that.setData({ topImage: res.data.lovely, timeList: res.data.seckillTime, active: res.data.seckillTimeIndex });
      let sec_length = res.data.seckillTime.length;

      this.setData({
        active: sec_length - 1
      })
      if (that.data.timeList.length) {
        that.setData({ scrollLeft: (that.data.active - 1.37) * 100 });
        that.setData({ seckillList: [], page: 1 });
        that.setData({ status: that.data.timeList[that.data.active].status });
        // that.getSeckillList();
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getIndexConfig();
    /**调用秒杀栏目 */
    // this.getSeckillConfig();
    //取消获取地址窗口
    // wxh.selfLocation(1);
    this.setData({
      navH: app.globalData.navHeight
    });
    if (options.spid) app.globalData.spid = options.spid;
    if (options.scene) app.globalData.code = decodeURIComponent(options.scene);
    if (wx.getStorageSync('msg_key')) this.setData({ iShidden: true });
    this.getTemlIds();
    this.getLiveList();
    /**设置全局高度 */
    this.setData({
      navH: app.globalData.navHeight
    });
  },
  getLiveList: function () {
    getLiveList(1, 20).then(res => {
      if (res.data.length == 1) {
        this.setData({ liveInfo: res.data[0] });
      } else {
        this.setData({ liveList: res.data });
      }
    }).catch(res => {

    })
  },
  /**
   * 商品详情跳转
   */
  goDetail: function (e) {
    let item = e.currentTarget.dataset.items
    if (item.activity && item.activity.type === "1") {
      wx.navigateTo({
        url: `/pages/activity/goods_seckill_details/index?id=${item.activity.id}&status=1`
      });
    } else if (item.activity && item.activity.type === "2") {
      wx.navigateTo({ url: `/pages/activity/goods_bargain_details/index?id=${item.activity.id}` });
    } else if (item.activity && item.activity.type === "3") {
      wx.navigateTo({
        url: `/pages/activity/goods_combination_details/index?id=${item.activity.id}`
      });
    } else {
      wx.navigateTo({ url: `/pages/goods_details/index?id=${item.id}` });
    }
  },
  getTemlIds() {
    let messageTmplIds = wx.getStorageSync(CACHE_SUBSCRIBE_MESSAGE);
    if (!messageTmplIds) {
      getTemlIds().then(res => {
        if (res.data)
          wx.setStorageSync(CACHE_SUBSCRIBE_MESSAGE, JSON.stringify(res.data));
      })
    }
  },
  catchTouchMove: function (res) {
    return false
  },
  onColse: function () {
    this.setData({ window: false });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getIndexConfig();
    if (app.globalData.isLog && app.globalData.token) this.get_issue_coupon_list();
  },
  get_issue_coupon_list: function () {
    var that = this;
    getCoupons({ page: 1, limit: 3 }).then(res => {
      that.setData({ couponList: res.data });
      if (!res.data.length) that.setData({ window: false });
    });
  },
  getIndexConfig: function (id = 0, page = 0) {
    var that = this;
    if (id) {
      this.setData({
        catId: id,
      });
    }
    getRuralList(id, {
      page: that.data.page,
      limit: that.data.limit,
    }).then(res => {
      if (page == 0) {
        that.setData({ seckillInfo: res.data.seckillInfo });
      } else {
        that.setData({ seckillInfo: that.data.seckillInfo.concat(res.data.seckillInfo) });
      }
      that.setData({
        imgUrls: res.data.banner,
        cateList: res.data.cateList,
        row_count: res.data.row_count,
        couponList: res.data.couponList,
        store: res.data.store,
        menus: res.data.menus
      });
      var imgUrls = res.data.banner;
      this.setData({
        color: imgUrls[0]['color']
      })
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.userInfo']) {
            that.setData({ window: that.data.couponList.length ? true : false });
          } else {
            that.setData({ window: false, iShidden: true });
          }
        }
      });
    }).catch(err => {
      that.setData({ loading: false, loadTitle: "加载更多" });
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ window: false });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getIndexConfig();
    if (app.globalData.isLog && app.globalData.token) this.get_issue_coupon_list();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let id = this.data.catId;
    let page = this.data.page;
    let self = this;
    if (self.data.seckillInfo.length >= self.data.row_count) {
      return;
    }
    this.data.page++;
    this.getIndexConfig(id, page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: that.data.store.name,
      path: '/pages/index/index',
      // imageUrl: that.data.storeInfo.image,
      success: function () {
        wx.showToast({
          title: '分享成功',
          icon: 'success',
          duration: 2000
        })
      }
    }
  }
})